package com.yunc.modules.act.service.impl;

import com.yunc.modules.act.service.IActivityConsumerService;
import org.activiti.engine.IdentityService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author taocongcong
 * @create 2019-02-18 11:16
 */
@Service("activityConsumerService")
public class ActivityConsumerServiceImpl implements IActivityConsumerService {

    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private IdentityService identityService;
    @Autowired
    private RepositoryService repositoryService;

    @Override
    public boolean startActivityDemo() {
        System.out.println("method startActivityDemo begin....");

        System.out.println( "调用流程存储服务，查询部署数量："
                + repositoryService.createDeploymentQuery().count());

        Map<String,Object> map = new HashMap<String,Object>();
        map.put("staff","wangwu");
        /*map.put("flag","0");*/
        // map.put("assignee","lisi");

        //流程启动
        identityService.setAuthenticatedUserId("wangwu");
        ExecutionEntity pi1 = (ExecutionEntity) runtimeService.startProcessInstanceByKey("myProcess", "123", map);

        List<Task> tq=taskService.createTaskQuery().taskAssignee("wangwu").list();
        System.out.println(tq.size());
        //当前任务办理人
        String assignee = "wangwu";
        //与任务相关的Service，创建一个任务查询对象
        List<Task> tasks = taskService
                .createTaskQuery()
                .taskAssignee(assignee)
                .list();
        if (tasks != null && tasks.size() > 0) {
            for (Task task : tasks) {
                System.out.println("任务ID:"+task.getId());
                System.out.println("任务的办理人:"+task.getAssignee());
                System.out.println("任务名称:"+task.getName());
                System.out.println("任务的创建时间:"+task.getCreateTime());
                System.out.println("流程实例ID:"+task.getProcessInstanceId());
                System.out.println("#####################################");
            }
        }
        System.out.println("method startActivityDemo end....");
        return false;
    }
}
